package com.qms.app.service.impl;

import com.qms.app.bean.Permissions;
import com.qms.app.bean.Roles;
import com.qms.app.dao.PermissionsDao;
import com.qms.app.dao.RolesDao;
import com.qms.app.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionsServiceImpl implements PermissionsService {
    @Autowired
    private PermissionsDao permissionsDao;
    @Autowired
    private RolesDao rolesDao;

    @Override
    public List<Permissions> listPermissions() {
        return permissionsDao.list();
    }

    @Override
    public int savePermissions(List<Integer> pids, int rid) {
//  前端自己的传入的参数 中携带对应的pid
//  也可以写成传入的参数是 HttpServletRequest   request
//  再对应的request中使用 String[] pids = request.getParameterValues("pid");
        Roles roles = rolesDao.get(rid);
        int size = roles.getPermissionsList().size();
        if (size > 0) {
            return -1;
        }
        int count = 0;
        // 对这个进行批量添加
        for (Integer pid : pids) {
            // 对角色进行添加权限
            count = permissionsDao.save(pid, rid);
        }
        return count;
    }

    @Override
    public int removePermissions(int rid) {
        Roles roles = rolesDao.get(rid);
        int size = roles.getPermissionsList().size();

        return size>0?permissionsDao.removePermissions(rid):-1;
    }
}
